//
//  CollectViewController.m
//  Everest
//
//  Created by 刘宇文 on 14-12-31.
//  Copyright (c) 2014年 ThreeL. All rights reserved.
//

#import "CollectViewController.h"
#import "DataBaseHandler.h"
#import "AlbumViewController.h"
#import "ClassifyDetail.h"

#define REUSE @"reuse"

@interface CollectViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, retain) NSMutableArray *arr;
@property (nonatomic, retain) UIAlertView *alertView;

@end

@implementation CollectViewController

- (void)dealloc
{
    [_alertView release];
    [_arr release];
    [_tableView release];
    [super dealloc];
}

- (void)viewWillAppear:(BOOL)animated
{
//    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    
        DataBaseHandler *handler = [DataBaseHandler shareInstance];
        
        self.arr = [[[NSMutableArray alloc] initWithArray:[handler seleteAll]] autorelease];
        
        if (self.arr.count == 0) {
            
            self.alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"您还没有收藏, 赶快去收藏吧!" delegate:self cancelButtonTitle:nil otherButtonTitles:nil, nil];
            [self.alertView show];
            [_alertView release];
            
            [NSTimer scheduledTimerWithTimeInterval:1.0f
                                             target:self
                                           selector:@selector(dismissAlert:)
                                           userInfo:nil
                                            repeats:NO];

        }
    
        
//    });
    [self.tableView reloadData];
}

- (void)dismissAlert:(NSTimer *)timer
{
    
    [self.alertView dismissWithClickedButtonIndex:0 animated:YES];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds];
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.tableFooterView = [[[UIView alloc] init] autorelease];
    [self.view addSubview:self.tableView];
    [_tableView release];
    
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:REUSE];
    
    self.navigationItem.rightBarButtonItem = self.editButtonItem;
    
    
}
// cell的点击方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    AlbumViewController *albumVC = [[AlbumViewController alloc] init];
    
    NSNumber *number = [[self.arr objectAtIndex:indexPath.row] albumId];
    
    ClassifyDetail *classify = [[ClassifyDetail alloc] init];
    
    classify.pageId = number;
    
    albumVC.classifyDetail = classify;
    
    [self.navigationController pushViewController:albumVC animated:YES];
    
    [classify release];
    [albumVC release];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.arr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:REUSE];
    
    cell.textLabel.text = [[self.arr objectAtIndex:indexPath.row] title];
    
    return cell;
}

// 系统导航栏编辑按钮的调用方法
- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
    [super setEditing:editing animated:animated];
    
    [self.tableView setEditing:editing animated:animated];
    
}


// 当点击删除按钮时, 会调用这个方法
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        
        
        DataBaseHandler *handler = [DataBaseHandler shareInstance];
        
        NSString *title = [[self.arr objectAtIndex:indexPath.row] title];
        
        [self.arr removeObjectAtIndex:indexPath.row];
        
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];
        
        [handler deleteTitle:title];
        
    }
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
